﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADDemo2025
{
    public class Command0209
    {
        [CommandMethod("Demo0209")]
        public void Demo0209()
        {
            if (!UmUserMethod.SelectEntities(out List<Curve> curs)) return;
            List<Polyline> pls=curs.FindAll(x=>x is Polyline).Cast<Polyline>().ToList();
            List<Circle> cirs=curs.FindAll(x=>x is Circle).Cast<Circle>().ToList();
            List<Point3d> pos = new List<Point3d>();
            foreach (Polyline poly in pls)
            {
                for (int i = 0; i < poly.NumberOfVertices; i++)
                {
                    pos.Add(poly.GetPoint3dAt(i));
                }
            }
            foreach (var cir in cirs)
            {
                pos.Add(cir.Center);
            }
            pos = pos.OrderBy(p => p.X).ThenBy(p => p.Y).ToList();
            for (int i = pos.Count - 2; i >= 0; i--)
            {
                if (pos[i].X == pos[i + 1].X) pos.RemoveAt(i);
            }

            Jig jig = new Jig(Point3d.Origin, (p, e1, e2) =>
            {
                int n = 0;
                for (int i = 1; i < pos.Count; i++)
                {
                    double rotation = 0;
                    Point3d line1Point = pos[0];
                    Point3d line2Point = pos[i];
                    Point3d dimensionLinePoint = p + new Vector3d(0, n * 4, 0);
                    n++;
                    string dimensionText = "";
                    Document doc = Application.DocumentManager.MdiActiveDocument;
                    ObjectId dimensionStyle = doc.Database.DimStyleTableId;
                    RotatedDimension rotatedDimension = new RotatedDimension(rotation, line1Point, line2Point, dimensionLinePoint, dimensionText, dimensionStyle);
                    e1.Add(rotatedDimension);
                    e2.Add(rotatedDimension);
                }
            });
        }
    }
}
